Page({

  /**
   * 页面的初始数据
   */
  data: {
    xx: null, //一级留言数组
    id: null, //这是plid
    flag: true,
    anonymous: 0,
    onelanguage: '',
    twolanguage: ''
  },
  show: function () {
    this.setData({
      flag: false
    })

  },
  hide: function () {
    this.setData({
      flag: true
    })
  },
  ly: function (e) {
    console.log(e)
    this.setData({
      onelanguage: e.detail.value
    })
  },
  fbly: function () {
    var that = this;
    wx.request({
      url: 'https://welcome.gongxueyun.com/welcome/yingxin/se.php',
      data: {
        language: that.data.onelanguage,
        xh: that.data.xh,
        anonymous: that.data.anonymous,
        md: 1
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.onLoad();
        console.log(res.data)
        that.setData({
          flag: true,
          value: '',
          onelanguage: ''
        })
      }
    })
  },
  plsr: function (e) {
    this.setData({
      twolanguage: e.detail.value
    })
  },
  pl: function (e) {
    var that = this;
    var res = wx.getStorageSync('dl')
    if (res) {
      wx.request({
        url: 'https://welcome.gongxueyun.com/welcome/yingxin/se.php',
        data: {
          language: that.data.twolanguage,
          plid: e.currentTarget.id,
          xh: that.data.xh,
          md: 2
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.onLoad();
          that.setData({
            value: '',
            twolanguage: ''
          })
        }
      })
    } else {
      wx.showToast({
        title: '评论请登陆',
        icon: 'none',
        duration: 1000
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    var res = wx.getStorageSync('dl')
    if (res) {
      that.setData({
        anonymous: 1,
        xh: res.xh,
        n: 0
      })
    } else {
      console.log('匿名')
    }
    wx.request({
      url: 'https://welcome.gongxueyun.com/welcome/yingxin/se.php',
      data: {
        md: 3
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var arr = [];
        for (var i in res.data) {
          if (res.data[i].anonymous == 1) {
            // console.log('---------')
            res.data[i].x = res.data[i].xm.slice(0, 1),
              res.data[i].m = res.data[i].xm.slice(1)
          } else {
            res.data[i].x = '匿',
              res.data[i].m = ''
          }
        }
        that.setData({
          xx: res.data,
          n: 1
        })
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onPullDownRefresh: function () {
    var that = this;
    wx.request({
      url: 'https://welcome.gongxueyun.com/welcome/yingxin/se.php',
      data: {
        md: 3,
        n: 0
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var arr = []
        for (var i in res.data) {
          if (res.data[i].anonymous == 1) {
            // console.log('---------')
            res.data[i].x = res.data[i].xm.slice(0, 1),
              res.data[i].m = res.data[i].xm.slice(1)
          } else {
            res.data[i].x = '匿',
              res.data[i].m = ''
          }
        }
        that.setData({
          xx: res.data,
          n: 1
        })
        console.log(res.data)
      },
      complete: function () {
        wx.stopPullDownRefresh()
      }
    })
  },

  onReachBottom: function () {
    var that = this;
    if (that.data.xx.length >= 20) {
      wx.showLoading({
        icon: 'loading',
        title: '加载数据中',
        mask: true
      })
      wx.request({
        url: 'https://welcome.gongxueyun.com/welcome/yingxin/se.php',
        data: {
          md: 3,
          n: that.data.n
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          wx.hideLoading();
          that.data.n += 1;
          var arr = [];
          if (res.data != '') {
            for (var i in res.data) {
              if (res.data[i].anonymous == 1) {
                // console.log('---------')
                res.data[i].x = res.data[i].xm.slice(0, 1),
                  res.data[i].m = res.data[i].xm.slice(1)
              } else {
                res.data[i].x = '匿',
                  res.data[i].m = ''
              }
              that.data.xx.push(res.data[i])
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: '到底啦！',
              duration: 2000
            })
            return
          }
          that.setData({
            xx: that.data.xx
          })
          console.log(that.data.xx)
        },
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '到底啦！',
        duration: 2000
      })
      return
    }
  },
})